package com.neusoft.adminweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.adminweb.entity.Admin;
import com.neusoft.adminweb.entity.Dorm;
import com.neusoft.adminweb.service.DormService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Dorm)表控制层
 *
 * @author makejava
 * @since 2020-11-09 10:29:13
 */
@Controller
@RequestMapping("dorm")
public class DormController implements BaseController<Dorm>{
    /**
     * 服务对象
     */
    @Resource
    private DormService dormService;
    private final String FOLDER = "pages/dorm/";

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Override
    @GetMapping("queryById")
    public Dorm queryById(Integer id) {
        return this.dormService.queryById(id);
    }

    @Override
    public String toAddOrEdit(Integer id, Model result) {
        //为添加准备的
        Dorm bean = new Dorm();
        if (id != null) {
            bean = dormService.queryById(id);
        }
        result.addAttribute("editBean", bean);
        return FOLDER + "addOrEdit";
    }

    @GetMapping("toAddOrEditWithParent")
    public String toAddOrEditWithParent(Integer id, Integer parent, Model result) {
        //为添加准备的
        Dorm bean = new Dorm();
        bean.setParent(parent);
        if (id != null) {
            bean = dormService.queryById(id);
        }
        result.addAttribute("editBean", bean);
        //从数据库中获取所有宿舍楼
        return FOLDER + "addOrEdit";
    }

    @Override
    public String save(Dorm bean, Model result) {
        System.out.println("save");
        if (bean != null) {
            if (bean.getId() == null) {
                //添加
                //数据合法性验证
                //用户名是否重复
                boolean insert = dormService.insert(bean);
                if (insert) {
                    result.addAttribute("msg", "添加失败");
                    return toAddOrEdit(null, result);
                }
            } else {
                //编辑
                boolean update = dormService.update(bean);
                if (update) {
                    result.addAttribute("msg", "添加失败");
                    return toAddOrEdit(bean.getId(), result);
                }
            }
        }

        return "redirect:/dorm/toList";
    }

    @Override
    public String toList(Integer pageNum, Integer pageSize, Model result) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Dorm> list = dormService.queryPageByArgs(null);
        System.out.println(list);
        PageInfo<Dorm> pageInfo = new PageInfo<>(list);
        result.addAttribute("pageInfo", pageInfo);

        return FOLDER + "list";
    }

    @Override
    public String search(Dorm args, Integer pageNum, Integer pageSize, Model result) {
        System.out.println("search:::args:" + args);
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        List<Dorm> list = dormService.queryPageByArgs(args);
        PageInfo<Dorm> pageInfo = new PageInfo<>(list);
        result.addAttribute("pageInfo", pageInfo);

//        return pageInfo;
        return FOLDER + "list";
    }

    @Override
    public String delete(Integer id) {
        if (id != null) {
            dormService.deleteById(id);
        }

        return "redirect:/dorm/toList";
    }

}